package com.huawei.secure.android.common.encrypt.hash;

import android.text.TextUtils;
import android.util.Log;
import com.huawei.secure.android.common.encrypt.utils.a;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public abstract class HMACSHA256 {
    private static final String TAG = "HMACSHA256";
    private static final String i = "";
    private static final String s = "HmacSHA256";
    private static final int t = 32;

    public static String hmacSHA256Encrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return "";
        }
        return hmacSHA256Encrypt(str, a.l(str2));
    }

    public static String hmacSHA256Encrypt(String str, byte[] bArr) {
        Exception e;
        if (TextUtils.isEmpty(str) || bArr == null) {
            return "";
        }
        if (bArr.length < 32) {
            Log.e(TAG, "hmac key length is not right");
            return "";
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr, s);
            Mac instance = Mac.getInstance(secretKeySpec.getAlgorithm());
            instance.init(secretKeySpec);
            return a.f(instance.doFinal(str.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e2) {
            e = e2;
            Log.e(TAG, "hmacsha256 encrypt exception" + e.getMessage());
            return "";
        } catch (InvalidKeyException e3) {
            e = e3;
            Log.e(TAG, "hmacsha256 encrypt exception" + e.getMessage());
            return "";
        } catch (NoSuchAlgorithmException e4) {
            e = e4;
            Log.e(TAG, "hmacsha256 encrypt exception" + e.getMessage());
            return "";
        }
    }
}
